package com.redoor.app.stake.workflow.mq.consumerService;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.redoor.app.stake.basicdata.model.constants.BasePropConstants;
import com.redoor.app.stake.basicdata.service.BasePropService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Properties;


/**
 * MQ 监听程序
 *
 *
 * Created by leoliu on 2017/8/29.
 */
@Service
public class FesOperationMQListenerService {


    private static final Logger logger = LoggerFactory.getLogger(FesOperationMQListenerService.class) ;

    @Autowired
    private BasePropService basePropService;

    private Consumer consumer ;

    @PostConstruct
    public void  init(){
        try {
            logger.info("init fesoperationMQListenerService ...");
            Properties cprop = new Properties();
            cprop.put(PropertyKeyConst.AccessKey, this.basePropService.getProp(BasePropConstants.consumerAccessKey));
            cprop.put(PropertyKeyConst.SecretKey, this.basePropService.getProp(BasePropConstants.consumerSecretKey));
            cprop.put(PropertyKeyConst.ONSAddr, this.basePropService.getProp(BasePropConstants.namesrvAddr));
            cprop.put(PropertyKeyConst.ConsumerId, this.basePropService.getProp(BasePropConstants.customerId2));
            if(basePropService.initMQFlag()) {
                consumer = ONSFactory.createConsumer(cprop);
                consumer.subscribe("TOPIC_FES_OPERATION", "completeDebug_tag", new FesOperation());
                consumer.start();
                logger.info("FesOperationMQListenerService_init_success_consumer");
            } else {
                logger.warn("not init FesOperationMQListenerService because flag is flase");
            }
        } catch (Exception e){
            logger.error("init FesOperationMQListenerService failure",e);
        }

    }

}
